package com.yilin.tms.order.service.party.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.core.entity.order.party.OrderAutLog;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.order.mapper.party.OrderAutLogMapper;
import com.yilin.tms.order.service.party.IOrderAutLogService;
import com.yilin.tms.core.remote.IUserRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * 订单操作日志Service
 *
 * @author zhangjinyuan
 * @date 2022/10/29
 */
@Service
@Transactional
public class OrderAutLogService extends ServiceImpl<OrderAutLogMapper, OrderAutLog> implements IOrderAutLogService {

    @Autowired
    IUserRemote userRemote;

    @Autowired
    OrderAutLogMapper orderAutLogMapper;

    /**
     * 保存抢单日志
     *
     * @param robOrderId 抢单Id
     * @param user       当前登录用户
     * @param title      日志标题
     * @param remark     日志内容
     */
    @Override
    public void saveRobAutLog(String robOrderId, Account user, String title, String remark) {
        saveOrderAutLog(robOrderId, null, user, title, remark);
    }

    /**
     * 保存运单日志
     *
     * @param transitOrderId 运单Id
     * @param user           当前登录用户
     * @param title          日志标题
     * @param remark         日志内容
     */
    @Override
    public void saveTransitAutLog(String transitOrderId, Account user, String title, String remark) {
        saveOrderAutLog(null, transitOrderId, user, title, remark);
    }

    private void saveOrderAutLog(String robOrderId, String transitOrderId, Account user, String title, String remark) {
        OrderAutLog orderAutLog = new OrderAutLog();
        orderAutLog.setFromAccountId(user.getId());
        orderAutLog.setFromName(user.getName());
        orderAutLog.setRobOrderId(robOrderId);
        orderAutLog.setTransitOrderId(transitOrderId);
        orderAutLog.setTitle(title);
        orderAutLog.setRemark(remark);
        orderAutLog.setAddUserId(user.getId());
        orderAutLog.setCreateTime(new Date());
        orderAutLog.setPlatId(user.getSuperiorPlatId());
        orderAutLog.setPlatName(user.getSuperiorPlatName());
        orderAutLogMapper.insert(orderAutLog);
    }
}
